package com.lumis.utils;

import java.io.File;

import org.springframework.stereotype.Component;

@Component
public class DirectoriosUtils {
	
	/** 
	 * Deletes the given path and, if it is a directory, deletes all its children. 
	 */  
	public boolean borrarConHijos(String path) {  
	    File file = new File(path);  
	    if (!file.exists()) {  
	        return true;  
	    }  
	    if (!file.isDirectory()) {  
	        return file.delete();  
	    }  
	    return this.borrarHijos(file) && file.delete();  
	}  
	  
	private boolean borrarHijos(File dir) {  
	    File[] children = dir.listFiles();  
	    boolean childrenDeleted = true;  
	    for (int i = 0; children != null && i < children.length; i++) {  
	        File child = children[i];  
	        if (child.isDirectory()) {  
	            childrenDeleted = this.borrarHijos(child) && childrenDeleted;  
	        }  
	        if (child.exists()) {  
	            childrenDeleted = child.delete() && childrenDeleted;  
	        }  
	    }  
	    return childrenDeleted;  
	}  

}
